package com.dz.risk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dz.risk.common.ResponseVO;
import com.dz.risk.common.SysConstant;
import com.dz.risk.dao.EarlyWarningDebtListDao;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.EarlyWarningDebtListEntity;
import com.dz.risk.pojo.domain.ListOfDefaultedBonds;
import com.dz.risk.service.EarlyWarningDebtListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("earlyWarningDebtListService")
public class EarlyWarningDebtListServiceImpl extends ServiceImpl<EarlyWarningDebtListDao, EarlyWarningDebtListEntity> implements EarlyWarningDebtListService {
    @Autowired
    private EarlyWarningDebtListDao earlyWarningDebtListDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(EarlyWarningDebtListEntity earlyWarningDebtListEntity) {

        earlyWarningDebtListDao.insert(earlyWarningDebtListEntity);
    }

    @Override
    public IPage<EarlyWarningDebtListEntity> query(QueryParams queryParams) {
        IPage<EarlyWarningDebtListEntity> page = new Page<>();
        page.setCurrent(queryParams.getPageNum());
        page.setSize(queryParams.getPageSize());
        QueryWrapper<EarlyWarningDebtListEntity> wrapper = new QueryWrapper<EarlyWarningDebtListEntity>();

        wrapper.lambda()
                .eq(!StringUtils.isEmpty(queryParams.getBeginDate()), EarlyWarningDebtListEntity::getBeginDate,queryParams.getBeginDate())
                .eq(!StringUtils.isEmpty(queryParams.getEndDate()),EarlyWarningDebtListEntity::getEndDate, queryParams.getEndDate())
                .eq(!StringUtils.isEmpty(queryParams.getBondCode()),EarlyWarningDebtListEntity::getBondCode, queryParams.getBondCode())
                .like(!StringUtils.isEmpty(queryParams.getBondName()),EarlyWarningDebtListEntity::getBondName, queryParams.getBondName())
                .orderByDesc(EarlyWarningDebtListEntity::getBondCode);

        IPage<EarlyWarningDebtListEntity> entityIPage = earlyWarningDebtListDao.selectPage(page, wrapper);
        return entityIPage;
    }

    @Override
    public List<EarlyWarningDebtListEntity> queryByVersion(Long version) {
       /* QueryWrapper<EarlyWarningDebtListEntity> wrapper = new QueryWrapper<EarlyWarningDebtListEntity>();

        if (StringUtils.hasLength(String.valueOf(version)) && !String.valueOf(SysConstant.ZERO).equals(version)){
            wrapper.le("begin_date", version).ge("end_date", version);
        }*/
        return earlyWarningDebtListDao.selectBetweenVersion(version);
    }

    @Override
    public List<EarlyWarningDebtListEntity> queryByCode(String bondCode) {
        QueryWrapper<EarlyWarningDebtListEntity> wrapper = new QueryWrapper<EarlyWarningDebtListEntity>();

        if (StringUtils.hasLength(String.valueOf(bondCode)) && !String.valueOf(SysConstant.ZERO).equals(bondCode)) {
            wrapper.eq("bond_code", bondCode).orderByDesc("bond_code");
        }
        return earlyWarningDebtListDao.selectList(wrapper);
    }

    @Override
    public ResponseVO add(EarlyWarningDebtListEntity earlyWarningDebtListEntity) {
        if (earlyWarningDebtListEntity.getBeginDate() > earlyWarningDebtListEntity.getEndDate()) {
            return ResponseVO.fail("开始时间不得大于结束时间");
        }
        earlyWarningDebtListDao.insert(earlyWarningDebtListEntity);
        return ResponseVO.ok();
    }

    @Override
    public void updateEarlyWarningDebtListEntityById(EarlyWarningDebtListEntity earlyWarningDebtListEntity) {
        earlyWarningDebtListDao.updateById(earlyWarningDebtListEntity);
    }

    @Override
    public void deleteById(String id) {
        earlyWarningDebtListDao.deleteById(id);
    }


}